{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python for Finance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Analyze Big Financial Data**\n",
    "\n",
    "O'Reilly (2014)\n",
    "\n",
    "Yves Hilpisch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img style=\"border:0px solid grey;\" src=\"http://hilpisch.com/python_for_finance.png\" alt=\"Python for Finance\" width=\"30%\" align=\"left\" border=\"0\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Buy the book ** |\n",
    "<a href='http://shop.oreilly.com/product/0636920032441.do' target='_blank'>O'Reilly</a> |\n",
    "<a href='http://www.amazon.com/Yves-Hilpisch/e/B00JCYHHJM' target='_blank'>Amazon</a>\n",
    "\n",
    "**All book codes & IPYNBs** |\n",
    "<a href=\"http://oreilly.quant-platform.com\">http://oreilly.quant-platform.com</a>\n",
    "\n",
    "**The Python Quants GmbH** | <a href='http://pythonquants.com' target='_blank'>www.pythonquants.com</a>\n",
    "\n",
    "**Contact us** | <a href='mailto:analytics@pythonquants.com'>analytics@pythonquants.com</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Types and Structures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Data Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Integers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "uuid": "16aa85ac-ab50-480b-bf7c-d49343bbe65a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 10\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "uuid": "2128044d-a150-4ea6-9aa3-6d6efb93bcf8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.bit_length()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "uuid": "19c4eea7-f4b6-48ac-a265-a5b5d61a838f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 100000\n",
    "a.bit_length()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "uuid": "34c9348e-fc24-4cb3-9f1e-538b517d16b7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "googol = 10 ** 100\n",
    "googol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "uuid": "55230cf3-5d00-4958-8ae9-2407ae6c968f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "333"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "googol.bit_length()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "uuid": "4f0ff5d0-e5aa-4825-8a0c-9db8297d8414"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 + 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "uuid": "3b44359c-ea14-4273-b275-f38b946955cb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 / 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "uuid": "1ee18247-5627-481a-8180-ba4ce158ebaf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(1 / 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Floats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "uuid": "3ba6e56a-a026-4eb7-ad01-0b7593ef5692"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.25"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1. / 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "uuid": "37a1e7ff-5949-4f0e-b4de-6ac28ee69e31"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type (1. / 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "uuid": "82d6e596-d9ec-49d9-85c4-6a8cb33b2a79"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = 0.35\n",
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "uuid": "a3f371c1-9a9a-4d19-bfac-3dc119486abb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.44999999999999996"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b + 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "uuid": "4cf2a3ed-6231-4672-9df1-03c60e3dfe7b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = 0.5\n",
    "c.as_integer_ratio()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "uuid": "af99f61f-afdb-41f8-bbf8-5abd0e7c9890"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3152519739159347, 9007199254740992)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.as_integer_ratio()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "uuid": "5584d3cc-2cdb-4ce9-9ac7-25541dc6e9b8"
   },
   "outputs": [],
   "source": [
    "import decimal\n",
    "from decimal import Decimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "uuid": "40822f81-0da3-4d62-9e31-713d0068de2f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[DivisionByZero, InvalidOperation, Overflow])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "decimal.getcontext()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "uuid": "775a8fc7-380f-4444-b394-ccdb6d8e6ea9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.09090909090909090909090909091')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = Decimal(1) / Decimal (11)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "uuid": "2a184e95-cc99-4b25-9905-b6d9becb2670"
   },
   "outputs": [],
   "source": [
    "decimal.getcontext().prec = 4  # lower precision than default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "uuid": "66cc3ae7-9a70-4612-b64c-a86fd481ef9d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.09091')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e = Decimal(1) / Decimal (11)\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "uuid": "6fa10fc7-4d21-4490-95a1-4a1be00f2c19"
   },
   "outputs": [],
   "source": [
    "decimal.getcontext().prec = 50  # higher precision than default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "uuid": "a3515ac1-4029-4645-ade9-094b6e3e87cf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.090909090909090909090909090909090909090909090909091')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = Decimal(1) / Decimal (11)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "uuid": "17a5e733-475b-4a49-a320-9b67bd45ce2e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.27272818181818181818181818181909090909090909090909')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = d + e + f\n",
    "g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "uuid": "01840910-c872-4f80-8e26-37d537aae1c5"
   },
   "outputs": [],
   "source": [
    "t = 'this is a string object'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "uuid": "d961e3c3-59ca-4e85-949d-553727b39477"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a string object'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "uuid": "443fd868-860a-4ea7-a26c-85ad658a2fb7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['this', 'is', 'a', 'string', 'object']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "uuid": "7ba8fdc2-a567-42a8-b363-847be15d5073"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.find('string')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "uuid": "8cc71c8c-e447-4272-8b82-74da5f504404"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.find('Python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "uuid": "ce57da4c-b95a-417f-a8bd-44f81b28d162"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'this|is|a|string|object'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.replace(' ', '|')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "uuid": "4a115a34-774e-4544-95eb-95e9ebec36ea"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'www.python.org'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'http://www.python.org'.strip('htp:/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "uuid": "8295f915-ee30-44df-896a-539aa6e5287d"
   },
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "uuid": "31a001d3-195c-42d3-823b-643eb2be3981"
   },
   "outputs": [],
   "source": [
    "series = \"\"\"\n",
    "'01/18/2014 13:00:00', 100, '1st';\n",
    "'01/18/2014 13:30:00', 110, '2nd';\n",
    "'01/18/2014 14:00:00', 120, '3rd'\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "uuid": "b62df3db-51da-4bd5-8abe-f536296c756e"
   },
   "outputs": [],
   "source": [
    "dt = re.compile(\"'[0-9/:\\s]+'\")  # datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "uuid": "02dd3264-3090-4607-a0d9-568d12093158"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'01/18/2014 13:00:00'\", \"'01/18/2014 13:30:00'\", \"'01/18/2014 14:00:00'\"]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = dt.findall(series)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "uuid": "ec54f07e-c507-437e-b5ec-6bd4f6d1e9f8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2014, 1, 18, 13, 0)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "pydt = datetime.strptime(result[0].replace(\"'\", \"\"),\n",
    "                         '%m/%d/%Y %H:%M:%S')\n",
    "pydt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "uuid": "28c72cd0-7efb-43b6-8dae-c48f034c154e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2014-01-18 13:00:00\n"
     ]
    }
   ],
   "source": [
    "print pydt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "uuid": "d166e305-a9ab-4d28-ac04-ee6016035b88"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'datetime.datetime'>\n"
     ]
    }
   ],
   "source": [
    "print type(pydt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Data Structures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false,
    "uuid": "a84deee3-7f60-459a-bb91-d0cd9c8031aa"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1, 2.5, 'data')\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "uuid": "9c63a354-9fde-4ded-92f2-bb8ed08ea185"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = 1, 2.5, 'data'\n",
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false,
    "uuid": "0c6923a3-efab-4cb1-baf7-f31ce725f520"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false,
    "uuid": "d36fef13-3878-4512-a458-9f5c2dda2cc6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false,
    "uuid": "d306c24a-5ae0-4395-9fb7-27bf0bb3d1a5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count('data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false,
    "uuid": "10a2d29d-b83d-45bf-aa6b-7d7d028e0fcf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.index(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "uuid": "434dd023-faaf-4b7f-953a-69ec45eec4aa"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'data'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [1, 2.5, 'data']\n",
    "l[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false,
    "uuid": "51232410-3f94-4df5-b982-ffcb8438c437"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2.5, 'data']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = list(t)\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "uuid": "7fab351f-6bb8-492f-a4be-314dfab9d13b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "uuid": "5f409ddb-67a5-42cc-949c-7b1cc7fb7489"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2.5, 'data', [4, 3]]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append([4, 3])  # append list at the end\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false,
    "uuid": "4e108298-4370-4371-9794-066aff49bfa3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2.5, 'data', [4, 3], 1.0, 1.5, 2.0]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.extend([1.0, 1.5, 2.0])  # append elements of list\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false,
    "uuid": "9076e4d2-518a-4554-b400-ffc06918a11a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'insert', 2.5, 'data', [4, 3], 1.0, 1.5, 2.0]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.insert(1, 'insert')  # insert object before index position\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "uuid": "7049a3a2-3d79-4584-bc57-3f91feb1e941"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'insert', 2.5, [4, 3], 1.0, 1.5, 2.0]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.remove('data')  # remove first occurence of object\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "uuid": "23bf5c19-5dc8-404a-bb5c-bda07c752d89"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'insert', 2.5, 1.0, 1.5, 2.0] [4, 3]\n"
     ]
    }
   ],
   "source": [
    "p = l.pop(3)  # removes and returns object at index\n",
    "print l, p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false,
    "uuid": "df15c54e-7500-40d8-9563-ecb305993e6b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2.5, 1.0, 1.5]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[2:5]  # 3rd to 5th element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Excursion: Control Structures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false,
    "uuid": "4661533f-3ff0-4699-a17b-cf061e860d8a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.25\n",
      "1.0\n",
      "2.25\n"
     ]
    }
   ],
   "source": [
    "for element in l[2:5]:\n",
    "    print element ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false,
    "uuid": "c5d97e03-6106-46a9-abb3-0c71cf0b7767"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = range(0, 8, 1)  # start, end, step width\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false,
    "uuid": "0ef58c5e-a1d8-438a-bf2f-db598b963187"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false,
    "uuid": "c1621dce-d184-4331-bd1d-6b6008286c99"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.25\n",
      "1.0\n",
      "2.25\n"
     ]
    }
   ],
   "source": [
    "for i in range(2, 5):\n",
    "    print l[i] ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false,
    "uuid": "474dd7ff-5fdd-429c-a574-48d698919e98"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 is odd\n",
      "2 is even\n",
      "3 is multiple of 3\n",
      "4 is even\n",
      "5 is odd\n",
      "6 is even\n",
      "7 is odd\n",
      "8 is even\n",
      "9 is multiple of 3\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    if i % 2 == 0:  # % is for modulo\n",
    "        print \"%d is even\" % i\n",
    "    elif i % 3 == 0:\n",
    "        print \"%d is multiple of 3\" % i\n",
    "    else:\n",
    "        print \"%d is odd\" % i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false,
    "uuid": "d15a40d3-e0c5-444c-83b8-68a08b1066de"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "while total < 100:\n",
    "    total += 1\n",
    "print total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false,
    "uuid": "bcfe427d-ea2c-485a-aace-f2927afefbdd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = [i ** 2 for i in range(5)]\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Excursion: Functional Programming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false,
    "uuid": "73fe24f8-e852-4b29-a488-901145b88120"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(x):\n",
    "    return x ** 2\n",
    "f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false,
    "uuid": "af1c90bc-8688-4b64-a269-11b81e53b7f2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def even(x):\n",
    "    return x % 2 == 0\n",
    "even(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false,
    "uuid": "6b9746e8-1717-4f33-a9d8-46fbb8d6d114"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, False, True, False, True, False, True, False, True, False]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "map(even, range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false,
    "uuid": "b8e6e5a8-704f-4d72-b92c-0c42f15b53e8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "map(lambda x: x ** 2, range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false,
    "uuid": "3354de29-f946-4076-a794-ba07969bf3b7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8, 10, 12, 14]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter(even, range(15)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false,
    "uuid": "5f490736-4c46-422f-8a6d-5e0c85104994"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reduce(lambda x, y: x + y, range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false,
    "uuid": "730dcd47-03f2-4c50-be10-196aed33d6a3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def cumsum(l):\n",
    "    total = 0\n",
    "    for elem in l:\n",
    "        total += elem\n",
    "    return total\n",
    "cumsum(range(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dicts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false,
    "uuid": "14981fb1-5636-4c73-93c9-f8e18930cc6b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\n",
    "     'Name' : 'Angela Merkel',\n",
    "     'Country' : 'Germany',\n",
    "     'Profession' : 'Chancelor',\n",
    "     'Age' : 60\n",
    "     }\n",
    "type(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false,
    "uuid": "786249fd-c888-4aa2-9157-973147ff2c2b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Angela Merkel 60\n"
     ]
    }
   ],
   "source": [
    "print d['Name'], d['Age']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "uuid": "33182240-3b00-4e43-922c-8189296791aa"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Country', 'Age', 'Profession', 'Name']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false,
    "uuid": "98b8e7a4-bac0-4299-9c47-c223269e24d7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Germany', 60, 'Chancelor', 'Angela Merkel']"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false,
    "uuid": "48545763-dfe7-4bb2-ade3-d0598498726e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Country', 'Germany'),\n",
       " ('Age', 60),\n",
       " ('Profession', 'Chancelor'),\n",
       " ('Name', 'Angela Merkel')]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false,
    "uuid": "9ed881cc-0eef-4307-b93b-0a728d98d647"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "61\n"
     ]
    }
   ],
   "source": [
    "birthday = True\n",
    "if birthday is True:\n",
    "    d['Age'] += 1\n",
    "print d['Age']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false,
    "uuid": "a2f40c35-348f-4727-87b6-df2e1c5f6f15"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Country', 'Germany')\n",
      "('Age', 61)\n",
      "('Profession', 'Chancelor')\n",
      "('Name', 'Angela Merkel')\n"
     ]
    }
   ],
   "source": [
    "for item in d.iteritems():\n",
    "    print item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false,
    "uuid": "2dc61967-ece2-48cd-af60-fcf58eb68270"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'str'>\n",
      "<type 'int'>\n",
      "<type 'str'>\n",
      "<type 'str'>\n"
     ]
    }
   ],
   "source": [
    "for value in d.itervalues():\n",
    "    print type(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false,
    "uuid": "7037930a-6fda-493e-8922-2095f50c76e5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'du', 'u', 'ud'}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set(['u', 'd', 'ud', 'du', 'd', 'du'])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false,
    "uuid": "c649fb45-81fd-4d46-9ea0-4462203fde81"
   },
   "outputs": [],
   "source": [
    "t = set(['d', 'dd', 'uu', 'u'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false,
    "uuid": "d592a20e-c8bd-4db4-b0db-f4f30b3ddb9a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'dd', 'du', 'u', 'ud', 'uu'}"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.union(t)  # all of s and t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false,
    "uuid": "0c60085c-d1e9-4023-9909-8f8623f1620e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'d', 'u'}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.intersection(t)  # both in s and t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false,
    "uuid": "c2efb482-02dc-41f6-ba00-4bbdccbfa905"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'du', 'ud'}"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.difference(t)  # in s but not t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false,
    "uuid": "ad2a4b31-8cea-41ef-af26-164ba264dd08"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dd', 'uu'}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.difference(s)  # in t but not s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false,
    "uuid": "1a040fc4-58d0-454d-b21a-59ce4325914d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dd', 'du', 'ud', 'uu'}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.symmetric_difference(t)  # in either one but not both"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false,
    "uuid": "a9173c67-0fff-4313-a1c6-c9f52f911b00"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from random import randint\n",
    "l = [randint(0, 10) for i in range(1000)]\n",
    "    # 1,000 random integers between 0 and 10\n",
    "len(l)  # number of elements in l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false,
    "uuid": "b79db3dd-9264-49c1-8163-ca5ac1dca35d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 10, 2, 6, 3, 3, 6, 9, 6, 3, 8, 3, 5, 8, 1, 3, 9, 6, 5, 10]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false,
    "uuid": "918b67ef-4b7e-456c-8133-60de3ae680f6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set(l)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NumPy Data Structures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Arrays with Python Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false,
    "uuid": "be5428db-f5f0-48d1-9cb3-46ab122778e7"
   },
   "outputs": [],
   "source": [
    "v = [0.5, 0.75, 1.0, 1.5, 2.0]  # vector of numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false,
    "uuid": "01678cdc-dfe0-481e-b9bc-b735b15c76c0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0]]"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = [v, v, v]  # matrix of numbers\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false,
    "uuid": "e2d1473a-4970-4085-9e8a-de65ed22f986"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.5, 0.75, 1.0, 1.5, 2.0]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false,
    "uuid": "e33ea433-6716-4d17-9ed3-04a68af6b489"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m[1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false,
    "uuid": "d905bfcc-e325-468e-829e-bb5c59def934"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[0.5, 1.5], [1, 2]], [[0.5, 1.5], [1, 2]]]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v1 = [0.5, 1.5]\n",
    "v2 = [1, 2]\n",
    "m = [v1, v2]\n",
    "c = [m, m]  # cube of numbers\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false,
    "uuid": "be03ed03-1e4a-428e-ac94-e451ceeaa745"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[1][1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false,
    "uuid": "015e5dd4-fda4-4572-9a23-06ff178ff2be"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0]]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = [0.5, 0.75, 1.0, 1.5, 2.0]\n",
    "m = [v, v, v]\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false,
    "uuid": "fd8ebed9-f11c-455a-a660-f4fe8d12d8f4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['Python', 0.75, 1.0, 1.5, 2.0],\n",
       " ['Python', 0.75, 1.0, 1.5, 2.0],\n",
       " ['Python', 0.75, 1.0, 1.5, 2.0]]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v[0] = 'Python'\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false,
    "uuid": "eacab892-e5a6-49be-9ef2-ef2972b313c3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0]]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from copy import deepcopy\n",
    "v = [0.5, 0.75, 1.0, 1.5, 2.0]\n",
    "m = 3 * [deepcopy(v), ]\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false,
    "uuid": "10ed3e78-fdc4-4adf-8f80-75eac48629a1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0],\n",
       " [0.5, 0.75, 1.0, 1.5, 2.0]]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v[0] = 'Python'\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Regular NumPy Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false,
    "uuid": "434dc849-a224-49f3-af1c-466dc48c9258"
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false,
    "uuid": "a58df3ba-f18e-459d-ac4d-6be84714a2a3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0, 0.5, 1.0, 1.5, 2.0])\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false,
    "uuid": "c6c73402-ed3b-4ea1-b62e-06dca996e713"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.5])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:2]  # indexing as with list objects in 1 dimension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false,
    "uuid": "f167bdb8-6673-4520-bd34-e2428ede6ad7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()  # sum of all elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false,
    "uuid": "0eafbb03-0cc4-4dd7-a177-9d8f2f8a32be"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.70710678118654757"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std()  # standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false,
    "uuid": "9eda9efe-4e85-4bc2-8df3-edd8ab40c7f2"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.5,  1.5,  3. ,  5. ])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum()  # running cumulative sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false,
    "uuid": "94ddb4ba-eb2b-4565-80cf-247db6dc5846"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  1.,  2.,  3.,  4.])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false,
    "uuid": "5c5f7bd8-b48b-4e8e-b6c8-95b67c9deed0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.  ,  0.25,  1.  ,  2.25,  4.  ])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false,
    "uuid": "eccae4a8-18fc-4362-8089-faed41fe558a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.70710678,  1.        ,  1.22474487,  1.41421356])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false,
    "uuid": "3d05357c-109d-45c7-b10b-fe6c958fd33d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0. ,  0.5,  1. ,  1.5,  2. ],\n",
       "       [ 0. ,  1. ,  2. ,  3. ,  4. ]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([a, a * 2])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": false,
    "uuid": "e536d513-ddb7-469d-9ad4-3abbc5cbf006"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.5,  1. ,  1.5,  2. ])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0]  # first row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false,
    "uuid": "d1d0c7dd-aadb-4217-b3ea-927f28f51300"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0, 2]  # third element of first row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "collapsed": false,
    "uuid": "41b3d64b-6121-4578-b33c-f9bef4d23ddb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15.0"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false,
    "uuid": "ba56b01a-cf24-4bee-be04-cdadd59a6cb5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  1.5,  3. ,  4.5,  6. ])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sum(axis=0)\n",
    "  # sum along axis 0, i.e. column-wise sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false,
    "uuid": "898c4a09-4488-4041-94a3-ea25cbd86637"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  5.,  10.])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sum(axis=1)\n",
    "  # sum along axis 1, i.e. row-wise sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "collapsed": false,
    "uuid": "6d7362b7-dda8-4961-8be1-96da9b1718cf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]],\n",
       "\n",
       "       [[0, 0, 0, 0],\n",
       "        [0, 0, 0, 0],\n",
       "        [0, 0, 0, 0]]], dtype=int32)"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.zeros((2, 3, 4), dtype='i', order='C')  # also: np.ones()\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false,
    "uuid": "0d4862fd-4847-4dd7-b8cd-e6966bbd5913"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1.0,  1.0,  1.0,  1.0],\n",
       "        [ 1.0,  1.0,  1.0,  1.0],\n",
       "        [ 1.0,  1.0,  1.0,  1.0]],\n",
       "\n",
       "       [[ 1.0,  1.0,  1.0,  1.0],\n",
       "        [ 1.0,  1.0,  1.0,  1.0],\n",
       "        [ 1.0,  1.0,  1.0,  1.0]]], dtype=float128)"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.ones_like(c, dtype='f16', order='C')  # also: np.zeros_like()\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false,
    "uuid": "cec48eba-20fa-45f0-8cb1-621efd2ce89b"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "I = 5000 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false,
    "uuid": "7da2200d-66ff-4442-be83-8879cb48fbcd"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 17.9 s, sys: 191 ms, total: 18.1 s\n",
      "Wall time: 18.1 s\n"
     ]
    }
   ],
   "source": [
    "%time mat = [[random.gauss(0, 1) for j in range(I)] for i in range(I)]\n",
    "  # a nested list comprehension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "collapsed": false,
    "uuid": "49ee3b79-8619-4ed3-a38b-2f2554f6434f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2.24 s, sys: 22 ms, total: 2.26 s\n",
      "Wall time: 2.22 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-4060.348546549695"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time reduce(lambda x, y: x + y,      \\\n",
    "     [reduce(lambda x, y: x + y, row) \\\n",
    "             for row in mat])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": false,
    "uuid": "54eee40d-6dc6-490a-964a-9a7b2e0c8fd0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.18 s, sys: 10 ms, total: 1.19 s\n",
      "Wall time: 1.19 s\n"
     ]
    }
   ],
   "source": [
    "%time mat = np.random.standard_normal((I, I))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": false,
    "uuid": "911c099e-6fc5-4c1e-bba2-e4eda476c808"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 16 ms, sys: 0 ns, total: 16 ms\n",
      "Wall time: 16.3 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "603.00152481953864"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time mat.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Structured Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false,
    "uuid": "15905219-d8fd-46f0-a1d9-2c2735d98b29"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([('Smith', 45, 1.8300000429153442, [0, 1]),\n",
       "       ('Jones', 53, 1.7200000286102295, [2, 2])], \n",
       "      dtype=[('Name', 'S10'), ('Age', '<i4'), ('Height', '<f4'), ('Children/Pets', '<i4', (2,))])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = np.dtype([('Name', 'S10'), ('Age', 'i4'),\n",
    "               ('Height', 'f'), ('Children/Pets', 'i4', 2)])\n",
    "s = np.array([('Smith', 45, 1.83, (0, 1)),\n",
    "              ('Jones', 53, 1.72, (2, 2))], dtype=dt)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false,
    "uuid": "9932654e-a73d-4cd6-a18e-5199f29efe44"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Smith', 'Jones'], \n",
       "      dtype='|S10')"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s['Name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false,
    "uuid": "10dc3d78-b6dc-4bf0-a4f1-1a4315bf0f2f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.7750001"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s['Height'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": false,
    "uuid": "1385e680-2a16-440d-a674-2ea5757fb41b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "53"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1]['Age']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vectorization of Code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Basic Vectorization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false,
    "uuid": "28bac5ac-be5d-4f60-acc6-f844259c5a22"
   },
   "outputs": [],
   "source": [
    "r = np.random.standard_normal((4, 3))\n",
    "s = np.random.standard_normal((4, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false,
    "uuid": "91d91253-168a-4ff2-8452-67e2cfd5c2f8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.38028957, -0.3166282 ,  1.11504775],\n",
       "       [ 0.99489346, -0.54511495, -1.55002302],\n",
       "       [-0.84395253,  0.18926218, -0.03662207],\n",
       "       [-2.65836367, -0.88675162,  0.7720991 ]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false,
    "uuid": "305b5877-2410-48e0-b5d3-d7f2f384e953"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.35879602,  4.8037457 ,  5.29685589],\n",
       "       [ 2.94133587,  0.92789766,  1.25101268],\n",
       "       [ 2.9786353 ,  3.74490908,  4.01759844],\n",
       "       [ 0.68076304,  0.83693791,  4.22983456]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * r + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": false,
    "uuid": "dd5f63cb-da0f-4397-8d86-315d115d4029"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.95236379,  0.54162337,  1.74921837],\n",
       "       [-1.66109387, -1.39630065, -0.27370323],\n",
       "       [-1.64244415,  0.01220506,  1.10958965],\n",
       "       [-2.79138028, -1.44178053,  1.21570771]])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = np.random.standard_normal(3)\n",
    "r + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "collapsed": false,
    "uuid": "cc58e76c-c3b1-495b-9cbe-8fb3fa92a59d"
   },
   "outputs": [],
   "source": [
    "# causes intentional error\n",
    "# s = np.random.standard_normal(4)\n",
    "# r + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false,
    "uuid": "011f4087-a2d6-4731-ac27-07f31d11f017"
   },
   "outputs": [],
   "source": [
    "# r.transpose() + s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "collapsed": false,
    "uuid": "88cab956-ec56-474e-8f2b-6c526da7d48c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape(r.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false,
    "uuid": "0f3b5cad-0146-4203-b158-238203ef5764"
   },
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return 3 * x + 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": false,
    "uuid": "08b395e9-13fc-43c1-8bba-28c63a216366"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.5"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(0.5)  # float object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false,
    "uuid": "1895e566-c5ee-4856-bca5-37fef37df0c5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.03819403,  7.70561856,  8.44528383],\n",
       "       [ 4.91200381,  1.89184649,  2.37651901],\n",
       "       [ 4.96795295,  6.11736362,  6.52639766],\n",
       "       [ 1.52114457,  1.75540686,  6.84475184]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(r)  # NumPy array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "collapsed": false,
    "uuid": "4087e02e-2fce-4656-8526-e13fa4fa2d96"
   },
   "outputs": [],
   "source": [
    "# causes intentional error\n",
    "# import math\n",
    "# math.sin(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false,
    "uuid": "11747287-8311-406e-afbe-6528f9aa94f1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.31513793,  0.78448972,  0.91212065],\n",
       "       [-0.02932786, -0.86039853, -0.76721884],\n",
       "       [-0.01068215,  0.36390277,  0.48712892],\n",
       "       [-0.91665069, -0.8826784 ,  0.57689096]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(r)  # array as input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false,
    "uuid": "0427534f-5b12-4de3-aee3-6bc5fba2051d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2246467991473532e-16"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(np.pi)  # float as input"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Memory Layout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false,
    "uuid": "f34c0c3d-112d-4ac4-bd48-21df98096047"
   },
   "outputs": [],
   "source": [
    "x = np.random.standard_normal((5, 10000000))\n",
    "y = 2 * x + 3  # linear equation y = a * x + b\n",
    "C = np.array((x, y), order='C')\n",
    "F = np.array((x, y), order='F')\n",
    "x = 0.0; y = 0.0  # memory clean-up"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "collapsed": false,
    "uuid": "f3fb83da-569c-423b-911e-9a2078d385b8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-0.19,  1.89,  1.06, ...,  0.03, -0.14,  0.92],\n",
       "        [-0.07, -0.2 ,  0.57, ...,  1.33, -0.23,  1.06],\n",
       "        [-0.02,  0.07, -0.96, ...,  0.29, -0.73, -0.  ],\n",
       "        [-0.1 , -0.75,  0.42, ...,  0.22,  0.41,  0.33],\n",
       "        [-0.24,  0.56, -0.38, ..., -2.06,  1.56, -0.04]],\n",
       "\n",
       "       [[ 2.61,  6.77,  5.12, ...,  3.07,  2.73,  4.83],\n",
       "        [ 2.87,  2.6 ,  4.14, ...,  5.66,  2.54,  5.13],\n",
       "        [ 2.97,  3.14,  1.09, ...,  3.58,  1.54,  2.99],\n",
       "        [ 2.81,  1.49,  3.84, ...,  3.44,  3.81,  3.65],\n",
       "        [ 2.52,  4.12,  2.24, ..., -1.11,  6.13,  2.92]]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C[:2].round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": false,
    "uuid": "a1a2af02-b1a0-4a41-a2e8-dd77b02ba796"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 63.1 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit C.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "collapsed": false,
    "uuid": "34e089f9-2877-48d5-b980-3a22949cb5e2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 63.4 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit F.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false,
    "uuid": "9ccbdca7-b257-432b-b609-c2f31ff181e1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 88.8 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit C[0].sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "collapsed": false,
    "uuid": "c681dab3-43d5-4247-bca5-7bb7ae41d821"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 32 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit C[0].sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": false,
    "uuid": "da5381da-b137-4da9-93f4-47d94f27b81d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 loops, best of 3: 618 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit F.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false,
    "uuid": "b31c6c3e-d720-4641-a6f9-8591b51b712e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 loops, best of 3: 1.84 s per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit F.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false,
    "uuid": "89201348-9a27-4883-b6b9-5f1caf291b61"
   },
   "outputs": [],
   "source": [
    "F = 0.0; C = 0.0  # memory clean-up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Further Reading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
    "\n",
    "<a href=\"http://www.pythonquants.com\" target=\"_blank\">www.pythonquants.com</a> | <a href=\"http://twitter.com/dyjh\" target=\"_blank\">@dyjh</a>\n",
    "\n",
    "<a href=\"mailto:analytics@pythonquants.com\">analytics@pythonquants.com</a>\n",
    "\n",
    "**Python Quant Platform** |\n",
    "<a href=\"http://oreilly.quant-platform.com\">http://oreilly.quant-platform.com</a>\n",
    "\n",
    "**Derivatives Analytics with Python** |\n",
    "<a href=\"http://www.derivatives-analytics-with-python.com\" target=\"_blank\">Derivatives Analytics @ Wiley Finance</a>\n",
    "\n",
    "**Python for Finance** |\n",
    "<a href=\"http://shop.oreilly.com/product/0636920032441.do\" target=\"_blank\">Python for Finance @ O'Reilly</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python2",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
